package com.li.temperature.chart.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.View;

import com.li.temperature.chart.data.DataModel;
import com.li.temperature.chart.data.Stopwatch;

public class TimeCountView extends View {

    private List<Double> mList;

    public static int WARN_TEMP = 40;
    private int matrixType;

    public TimeCountView(Context context) {
        super(context);
        mList = new ArrayList<Double>();
    }

    public TimeCountView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TimeCountView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private Paint mPaint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Stopwatch stopwatch = getStopwatch();
        if (stopwatch.isRunning()) {
            /*mPaint.setTextSize(45);
            mPaint.setColor(Color.WHITE);
            FontMetrics fontMetrics = mPaint.getFontMetrics();
            float top = Math.abs(fontMetrics.top);

            int indexUI = 1;
            if (matrixType != 0) {
                canvas.drawText(matrixType + " s 后", 0, top * indexUI, mPaint);
            }

            for (int i = 0; i < mList.size(); i++) {
                double value = mList.get(i);

                if (value > WARN_TEMP) {
                    mPaint.setColor(Color.RED);
                } else {
                    mPaint.setColor(Color.WHITE);
                }

                indexUI = indexUI + 1;
                String reusult = String.format("Y%d : %.1f ℃", (i + 1), value);

                canvas.drawText(reusult, 0, top * indexUI, mPaint);
            }*/

            postInvalidateOnAnimation();
        }
    }

    /**
     * Start the animation if it is not currently running.
     */
    public void update() {
        postInvalidateOnAnimation();
    }

    private Stopwatch getStopwatch() {
        return DataModel.getDataModel().getStopwatch();
    }

    public void updateResult(List<Double> list, int step) {
        matrixType = step;

        if (list != null) {
            mList.clear();
            mList.addAll(list);
        }
        update();
    }
}
